module MapperHelper
  def fragment_stats
    total = @fragment.concepts.count
    mapped = @fragment.concepts.mapped.count
    imported = @fragment.concepts.imported_mappings.count
    content_tag "span", "#{total}/#{mapped}(#{mapped - imported})"
  end

  def mapped_logo(concept)
    logo = ""
    concept.mappings.each do |c2|
      mapping_details = concept.mapping_details_for(c2)
      unless logo.include?("I")
#        logo << "I" if concept.mapping_details_for(c2).is_imported?
        logo << "I" if mapping_details.select {|m| m.is_imported?}.size > 0
      end

      unless logo.include?("M")
#        logo << "M" unless concept.mapping_details_for(c2).is_imported?
        logo << "M" if mapping_details.select {|m| !m.is_imported?}.size > 0
      end

      if logo.include?("I") && logo.include?("M")
        logo = "I/M"
      end
    end

    return logo
  end

  def sort_results_url(sort_by = "")
    url_for :controller => 'fragments',
            :action => 'search',
            :site_fragment_id => @site_fragment.id,
            :search_selected_concept_id => @selected_concept.id,
            :search_text => @search_text,
            :sort_by => sort_by,
            :include_attrs => @include_attrs,
            :dir => @sort_by == sort_by ? toggle_dir : "ASC" # only toggle the sort order if we sort the same column more than once
  end

  def toggle_dir
    if !@dir.blank? && @dir == "ASC"
      return "DESC"
    else
      return "ASC"
    end
  end

  def sort_arrow(sort_by = "")
    if @sort_by == sort_by
      if @dir == "ASC"
        image_tag "down-arrow.png", :style => "padding-left: 3px;"
      else
        image_tag "up-arrow.png", :style => "padding-left: 3px;"
      end
    end
  end
end
